const extend = require('extend');
const Buff = require('./buff');

const { game: gameConfig } = require('./config');

const CARD_NUMBER = [
    '', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '⒑', 'J', 'Q', 'K'
];
const CARD_SUIT = {
    spade: '♠',
    heart: '♥',
    club: '♣',
    diamond: '♦',
}
const CARD_COLOR = {
    red: 'red',
    black: 'black',
}
const CARD_ICON = {
    grow: 'fa-heart',
    kill: 'fa-rocket',
    fire: 'fa-fire',
    reinforce: 'fa-group',
    ice: 'fa-snowflake-o',
    vkill: 'fa-crosshairs',
    pest: 'fa-flask',
    tempt: 'fa-heartbeat',
    confusion: 'fa-tripadvisor',
    shield: 'fa-shield',
    peace: 'fa-podcast',
    quiet: 'fa-deaf',
    index: 'fa-heart'
}

const CARDS = {
    index: {
        type: 'index',
        option: {
            color: CARD_COLOR.red,
            suit: CARD_SUIT.heart,
            icon: CARD_ICON.index,
        },
        handler: {
            desc(card) {
                return [
                    '第',
                    '(' + card.option.value + ')',
                    '位出牌'
                ];
            }
        }
    },
    grow: {
        type: 'grow',
        option: {
            color: CARD_COLOR.red,
            suit: CARD_SUIT.heart,
            icon: CARD_ICON.grow,
            target: gameConfig.targets.ownHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                targetHomes[0].grow(card.option.value);
            },
            desc(card) {
                return [
                    '(立即)',
                    '为',
                    '[己方堡垒]',
                    '增加',
                    `{${card.option.value}点}`,
                    '[兵力]',
                    '。'
                ]
            }
        }
    },
    
    kill: {
        type: 'kill',
        option: {
            color: CARD_COLOR.black,
            suit: CARD_SUIT.spade,
            icon: CARD_ICON.kill,
            target: gameConfig.targets.targetHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                let count = targetHomes[0].kill(card.option.value);
                let shieldCount = 5 - count;
                if (shieldCount) {
                    ownHome.buff(shieldCount, Buff.type.shield, ownRole);
                }
            },
            desc(card) {
                return [
                    '<消灭>',
                    '[敌方堡垒]',
                    '中',
                    `{${card.option.value}点}`,
                    '[兵力]',
                    '，同时为',
                    '[己方堡垒]',
                    '中最多',
                    '{5点}',
                    '<正常>',
                    '[兵力]',
                    '附加',
                    '(持续一回合)',
                    '的',
                    '<护盾>',
                    '状态，每成功',
                    '<消灭>',
                    '{1点}',
                    '[兵力]',
                    '，',
                    '<护盾>',
                    '状态少附加',
                    '{1点}',
                    '[兵力]',
                    '。'
                ]
            }
        }
    },

    vkill: {
        type: 'vkill',
        option: {
            color: CARD_COLOR.black,
            suit: CARD_SUIT.spade,
            icon: CARD_ICON.vkill,
            target: gameConfig.targets.targetHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                if (targetHomes[0].troops.length < card.option.value) {
                    let count = Math.floor((card.option.value - targetHomes[0].troops.length) / 2);
                    targetHomes[0].kill(card.option.value);
                    if (count) {
                        ownHome.addReinforce(ownRole, count);
                    }
                } else {
                    targetHomes[0].kill(card.option.value);
                    targetHomes[0].addReinforce(ownRole, Math.floor(card.option.value / 2));
                }
            },
            desc(card) {
                return [
                    '<消灭>',
                    '[敌方堡垒]',
                    '中',
                    `{${card.option.value}点}`,
                    '[兵力]',
                    '，若',
                    '[敌方堡垒]',
                    '中',
                    '[兵力]',
                    '不足',
                    `{${card.option.value}点}`,
                    '，则多余点数的',
                    '{半数(向下取整)}',
                    '将转化为',
                    '[己方堡垒]',
                    '的',
                    '<增援>',
                    '点数',
                    '，否则',
                    '[敌方堡垒]',
                    '将增加',
                    `{${Math.floor(card.option.value / 2)}点}`,
                    '<增援>',
                    '。'
                ]
            }
        }
    },

    fire: {
        type: 'fire',
        option: {
            color: CARD_COLOR.red,
            suit: CARD_SUIT.diamond,
            icon: CARD_ICON.fire,
            target: gameConfig.targets.home,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                if (targetHomes[0] === ownHome) {
                    //对己方堡垒使用
                    targetHomes[0].debuff(card.option.value, Buff.type.ice);
                } else {
                    //对地方堡垒使用
                    targetHomes[0].buff(card.option.value, Buff.type.fire, ownRole);
                }
            },
            desc(card) {
                return [
                    '对',
                    '[敌方堡垒]',
                    '使用时，使敌方最多',
                    `{${card.option.value}点}`,
                    '<正常>',
                    '[兵力]',
                    '获得',
                    '<燃烧>',
                    '状态，对',
                    '[己方堡垒]',
                    '使用时，使己方最多',
                    `{${card.option.value}点}`,
                    '<冰冷>',
                    '状态的',
                    '[兵力]',
                    '解除其',
                    '<冰冷>',
                    '状态。'
                ]
            }
        }
    },

    fireall: {
        type: 'fireall',
        option: {
            color: CARD_COLOR.red,
            suit: CARD_SUIT.diamond,
            icon: CARD_ICON.fire,
            target: gameConfig.targets.targetHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                targetHomes[0].buff(card.option.value, Buff.type.fire, ownRole);
                ownHome.debuff(card.option.value, Buff.type.ice);
            },
            desc(card) {
                return [
                    '使',
                    '[敌方堡垒]',
                    '中最多',
                    `{${card.option.value}点}`,
                    '<正常>',
                    '[兵力]',
                    '获得',
                    '<燃烧>',
                    '状态，同时使',
                    '[己方堡垒]',
                    '中最多',
                    `{${card.option.value}点}`,
                    '<冰冷>',
                    '状态的',
                    '[兵力]',
                    '解除其',
                    '<冰冷>',
                    '状态。'
                ]
            }
        }
    },

    ice: {
        type: 'ice',
        option: {
            color: CARD_COLOR.black,
            suit: CARD_SUIT.club,
            icon: CARD_ICON.ice,
            target: gameConfig.targets.home,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                if (targetHomes[0] === ownHome) {
                    //对己方堡垒使用
                    targetHomes[0].debuff(card.option.value, Buff.type.fire);
                } else {
                    //对地方堡垒使用
                    targetHomes[0].buff(card.option.value, Buff.type.ice, ownRole);
                }
            },
            desc(card) {
                return [
                    '对',
                    '[敌方堡垒]',
                    '使用时，使敌方最多',
                    `{${card.option.value}点}`,
                    '<正常>',
                    '[兵力]',
                    '获得',
                    '<冰冷>',
                    '状态，对',
                    '[己方堡垒]',
                    '使用时，使己方最多',
                    `{${card.option.value}点}`,
                    '<燃烧>',
                    '状态的',
                    '[兵力]',
                    '解除其',
                    '<燃烧>',
                    '状态。'
                ]
            }
        }
    },

    iceall: {
        type: 'iceall',
        option: {
            color: CARD_COLOR.black,
            suit: CARD_SUIT.club,
            icon: CARD_ICON.ice,
            target: gameConfig.targets.targetHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                targetHomes[0].buff(card.option.value, Buff.type.ice, ownRole);
                ownHome.debuff(card.option.value, Buff.type.fire);
            },
            desc(card) {
                return [
                    '使',
                    '[敌方堡垒]',
                    '中最多',
                    `{${card.option.value}点}`,
                    '<正常>',
                    '[兵力]',
                    '获得',
                    '<冰冷>',
                    '状态，同时使',
                    '[己方堡垒]',
                    '中最多',
                    `{${card.option.value}点}`,
                    '<燃烧>',
                    '状态的',
                    '[兵力]',
                    '解除其',
                    '<燃烧>',
                    '状态。'
                ]
            }
        }
    },

    reinforce: {
        type: 'reinforce',
        option: {
            color: CARD_COLOR.red,
            suit: CARD_SUIT.heart,
            icon: CARD_ICON.reinforce,
            target: gameConfig.targets.ownHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                targetHomes[0].grow(card.option.value);
                targetHomes[0].addReinforce(ownRole, 5);
            },
            desc(card) {
                return [
                    '(立即)',
                    '为',
                    '[己方堡垒]',
                    '增加',
                    `{${card.option.value}点}`,
                    '[兵力]',
                    '，同时为',
                    '[己方堡垒]',
                    '增加',
                    `{5点}`,
                    '<增援>',
                    '。'
                ];
            }
        }
    },

    pest: {
        type: 'pest',
        option: {
            color: CARD_COLOR.black,
            suit: CARD_SUIT.club,
            icon: CARD_ICON.pest,
            target: gameConfig.targets.targetHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                targetHomes[0].buff(card.option.value, Buff.type.pest, ownRole);
            },
            desc(card) {
                return [
                    '使',
                    '[敌方堡垒]',
                    '最多',
                    `{${card.option.value}点}`,
                    '[兵力]',
                    '获得',
                    '<瘟疫>',
                    '状态。'
                ];
            }
        }
    },

    tempt: {
        type: 'tempt',
        option: {
            color: CARD_COLOR.red,
            suit: CARD_SUIT.diamond,
            icon: CARD_ICON.tempt,
            target: gameConfig.targets.targetHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                targetHomes[0].buff(card.option.value, Buff.type.tempt, ownRole);
            },
            desc(card) {
                return [
                    '使',
                    '[敌方堡垒]',
                    '最多',
                    `{${card.option.value}点}`,
                    '[兵力]',
                    '获得',
                    '<魅惑>',
                    '状态。'
                ];
            }
        }
    },

    confusion: {
        type: 'confusion',
        option: {
            color: CARD_COLOR.black,
            suit: CARD_SUIT.club,
            icon: CARD_ICON.confusion,
            target: gameConfig.targets.targetHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                targetHomes[0].buff(card.option.value, Buff.type.confusion, ownRole);
            },
            desc(card) {
                return [
                    '使',
                    '[敌方堡垒]',
                    '最多',
                    `{${card.option.value}点}`,
                    '[兵力]',
                    '获得',
                    '<混乱>',
                    '状态。'
                ];
            }
        }
    },

    peace: {
        type: 'peace',
        option: {
            color: CARD_COLOR.red,
            suit: CARD_SUIT.diamond,
            icon: CARD_ICON.peace,
            target: gameConfig.targets.ownHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                targetHomes[0].debuff(card.option.value);
            },
            desc(card) {
                return [
                    '解除',
                    '[己方堡垒]',
                    '最多',
                    `{${card.option.value}点}`,
                    '[兵力]',
                    '的减益状态。'
                ];
            }
        }
    },

    quiet: {
        type: 'quiet',
        option: {
            color: CARD_COLOR.red,
            suit: CARD_SUIT.diamond,
            icon: CARD_ICON.quiet,
            target: gameConfig.targets.ownHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                targetHomes[0].buff(card.option.value, Buff.type.quiet, ownRole);
            },
            desc(card) {
                return [
                    '为',
                    '[己方堡垒]',
                    '最多',
                    `{${card.option.value}点}`,
                    '<正常>',
                    '[兵力]',
                    '的增加',
                    '<宁静>',
                    '状态，状态',
                    '(持续一回合)',
                    '。'
                ];
            }
        }
    },

    shield: {
        type: 'shield',
        option: {
            color: CARD_COLOR.black,
            suit: CARD_SUIT.club,
            icon: CARD_ICON.shield,
            target: gameConfig.targets.ownHome,
            tick: 3,
        },
        handler: {
            use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
                targetHomes[0].buff(card.option.value, Buff.type.shield, ownRole);
            },
            desc(card) {
                return [
                    '为',
                    '[己方堡垒]',
                    '最多',
                    `{${card.option.value}点}`,
                    '<正常>',
                    '[兵力]',
                    '的增加',
                    '<护盾>',
                    '状态，状态',
                    '(持续一回合)',
                    '。'
                ];
            }
        }
    }
}

class VsCard {
    constructor(type, option, handler) {
        this.type = type;
        this.option = extend(true, option || {}, CARDS[type] && CARDS[type].option || {});
        this.handler = extend(true, handler || {}, CARDS[type] && CARDS[type].handler || {});
    }

    use(board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
        return this.handler.use(this, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes);
    }

    get data() {
        return {
            type: this.type,
            option: this.option,
            desc: this.handler.desc(this)
        }
    }
}

//黑桃
VsCard.KILL_01 = new VsCard(CARDS.kill.type, { number: CARD_NUMBER[1], value: 1 });
VsCard.KILL_02 = new VsCard(CARDS.kill.type, { number: CARD_NUMBER[2], value: 2 });
VsCard.KILL_03 = new VsCard(CARDS.kill.type, { number: CARD_NUMBER[3], value: 3 });
VsCard.KILL_04 = new VsCard(CARDS.kill.type, { number: CARD_NUMBER[4], value: 4 });
VsCard.KILL_05 = new VsCard(CARDS.kill.type, { number: CARD_NUMBER[5], value: 5 });
VsCard.VKILL_06 = new VsCard(CARDS.vkill.type, { number: CARD_NUMBER[6], value: 6 });
VsCard.VKILL_07 = new VsCard(CARDS.vkill.type, { number: CARD_NUMBER[7], value: 7 });
VsCard.VKILL_08 = new VsCard(CARDS.vkill.type, { number: CARD_NUMBER[8], value: 8 });
VsCard.VKILL_09 = new VsCard(CARDS.vkill.type, { number: CARD_NUMBER[9], value: 9 });
VsCard.VKILL_10 = new VsCard(CARDS.vkill.type, { number: CARD_NUMBER[10], value: 10 });

//红桃
VsCard.REINFORCE_01 = new VsCard(CARDS.reinforce.type, { number: CARD_NUMBER[1], value: 1 });
VsCard.REINFORCE_02 = new VsCard(CARDS.reinforce.type, { number: CARD_NUMBER[2], value: 2 });
VsCard.REINFORCE_03 = new VsCard(CARDS.reinforce.type, { number: CARD_NUMBER[3], value: 3 });
VsCard.REINFORCE_04 = new VsCard(CARDS.reinforce.type, { number: CARD_NUMBER[4], value: 4 });
VsCard.REINFORCE_05 = new VsCard(CARDS.reinforce.type, { number: CARD_NUMBER[5], value: 5 });
VsCard.GROW_06 = new VsCard(CARDS.grow.type, { number: CARD_NUMBER[6], value: 6 });
VsCard.GROW_07 = new VsCard(CARDS.grow.type, { number: CARD_NUMBER[7], value: 7 });
VsCard.GROW_08 = new VsCard(CARDS.grow.type, { number: CARD_NUMBER[8], value: 8 });
VsCard.GROW_09 = new VsCard(CARDS.grow.type, { number: CARD_NUMBER[9], value: 9 });
VsCard.GROW_10 = new VsCard(CARDS.grow.type, { number: CARD_NUMBER[10], value: 10 });

//梅花
VsCard.PEST_01 = new VsCard(CARDS.pest.type, { number: CARD_NUMBER[1], value: 1 });
VsCard.ICEALL_02 = new VsCard(CARDS.iceall.type, { number: CARD_NUMBER[2], value: 2 });
VsCard.PEST_03 = new VsCard(CARDS.pest.type, { number: CARD_NUMBER[3], value: 3 });
VsCard.ICEALL_04 = new VsCard(CARDS.iceall.type, { number: CARD_NUMBER[4], value: 4 });
VsCard.CONFUSION_05 = new VsCard(CARDS.confusion.type, { number: CARD_NUMBER[5], value: 5 });
VsCard.ICE_06 = new VsCard(CARDS.ice.type, { number: CARD_NUMBER[6], value: 6 });
VsCard.SHIELD_07 = new VsCard(CARDS.shield.type, { number: CARD_NUMBER[7], value: 7 });
VsCard.ICE_08 = new VsCard(CARDS.ice.type, { number: CARD_NUMBER[8], value: 8 });
VsCard.SHIELD_09 = new VsCard(CARDS.shield.type, { number: CARD_NUMBER[9], value: 9 });
VsCard.CONFUSION_10 = new VsCard(CARDS.confusion.type, { number: CARD_NUMBER[10], value: 10 });

//方片
VsCard.TEMPT_01 = new VsCard(CARDS.tempt.type, { number: CARD_NUMBER[1], value: 1 });
VsCard.FIREALL_02 = new VsCard(CARDS.fireall.type, { number: CARD_NUMBER[2], value: 2 });
VsCard.TEMPT_03 = new VsCard(CARDS.tempt.type, { number: CARD_NUMBER[3], value: 3 });
VsCard.FIREALL_04 = new VsCard(CARDS.fireall.type, { number: CARD_NUMBER[4], value: 4 });
VsCard.QUIET_05 = new VsCard(CARDS.quiet.type, { number: CARD_NUMBER[5], value: 5 });
VsCard.FIRE_06 = new VsCard(CARDS.fire.type, { number: CARD_NUMBER[6], value: 6 });
VsCard.PEACE_07 = new VsCard(CARDS.peace.type, { number: CARD_NUMBER[7], value: 7 });
VsCard.FIRE_08 = new VsCard(CARDS.fire.type, { number: CARD_NUMBER[8], value: 8 });
VsCard.PEACE_09 = new VsCard(CARDS.peace.type, { number: CARD_NUMBER[9], value: 9 });
VsCard.QUIET_10 = new VsCard(CARDS.quiet.type, { number: CARD_NUMBER[10], value: 10 });


VsCard.indexCard = (index) => new VsCard(CARDS.index.type, { number: CARD_NUMBER[index + 1], value: index + 1 });

VsCard.normalCards = [
    VsCard.KILL_01,
    VsCard.REINFORCE_01,
    VsCard.PEST_01,
    VsCard.TEMPT_01,

    VsCard.KILL_02,
    VsCard.REINFORCE_02,
    VsCard.ICEALL_02,
    VsCard.FIREALL_02,

    VsCard.KILL_03,
    VsCard.REINFORCE_03,
    VsCard.PEST_03,
    VsCard.TEMPT_03,

    VsCard.KILL_04,
    VsCard.REINFORCE_04,
    VsCard.ICEALL_04,
    VsCard.FIREALL_04,

    VsCard.KILL_05,
    VsCard.REINFORCE_05,
    VsCard.CONFUSION_05,
    VsCard.QUIET_05,

    VsCard.VKILL_06,
    VsCard.GROW_06,
    VsCard.ICE_06,
    VsCard.FIRE_06,

    VsCard.VKILL_07,
    VsCard.GROW_07,
    VsCard.SHIELD_07,
    VsCard.PEACE_07,

    VsCard.VKILL_08,
    VsCard.GROW_08,
    VsCard.ICE_08,
    VsCard.FIRE_08,

    VsCard.VKILL_09,
    VsCard.GROW_09,
    VsCard.SHIELD_09,
    VsCard.PEACE_09,

    VsCard.VKILL_10,
    VsCard.GROW_10,
    VsCard.CONFUSION_10,
    VsCard.QUIET_10,
];

VsCard.random = (cards, role) => {
    let index = Math.floor(Math.random() * cards.length);
    let card = cards[index];
    cards.splice(index, 1);
    return card;
}

const ADV_CARDS = [
    {
        color: CARD_COLOR.black,
        suit: CARD_SUIT.spade,
        number: CARD_NUMBER[11], 
        value: 1
    },
    {
        color: CARD_COLOR.black,
        suit: CARD_SUIT.spade,
        number: CARD_NUMBER[12], 
        value: 2
    },
    {
        color: CARD_COLOR.black,
        suit: CARD_SUIT.spade,
        number: CARD_NUMBER[13], 
        value: 3
    },

    {
        color: CARD_COLOR.red,
        suit: CARD_SUIT.heart,
        number: CARD_NUMBER[11], 
        value: 1
    },
    {
        color: CARD_COLOR.red,
        suit: CARD_SUIT.heart,
        number: CARD_NUMBER[12], 
        value: 2
    },
    {
        color: CARD_COLOR.red,
        suit: CARD_SUIT.heart,
        number: CARD_NUMBER[13], 
        value: 3
    },

    {
        color: CARD_COLOR.black,
        suit: CARD_SUIT.club,
        number: CARD_NUMBER[11], 
        value: 1
    },
    {
        color: CARD_COLOR.black,
        suit: CARD_SUIT.club,
        number: CARD_NUMBER[12], 
        value: 2
    },
    {
        color: CARD_COLOR.black,
        suit: CARD_SUIT.club,
        number: CARD_NUMBER[13], 
        value: 3
    },

    {
        color: CARD_COLOR.red,
        suit: CARD_SUIT.diamond,
        number: CARD_NUMBER[11], 
        value: 1
    },
    {
        color: CARD_COLOR.red,
        suit: CARD_SUIT.diamond,
        number: CARD_NUMBER[12], 
        value: 2
    },
    {
        color: CARD_COLOR.red,
        suit: CARD_SUIT.diamond,
        number: CARD_NUMBER[13], 
        value: 3
    }
]
VsCard.occupationCards = (occupation) => {
    return ADV_CARDS.filter((option, index) => occupation.cards.length > index).map((option, index) => new VsCard(occupation.type + '.' + option.number, extend(true, {icon: 'fa-magic'}, occupation.cards[index].option, option), occupation.cards[index].handler));
}

module.exports = VsCard;